<?php

namespace zhtfrontend\models\datas;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use common\components\extenders\MenuManage;

/**
 * This is the model class for table "{{%channel_distribution}}".
 *
 * @property integer $id
 * @property string $name
 * @property string $contacts
 * @property string $mobile
 * @property string $admin
 * @property integer $admin_id
 * @property integer $created_at
 */
class ChannelDistribution extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%channel_distribution}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['admin_id', 'created_at'], 'integer'],
            [['name'], 'string', 'max' => 50],
            [['contacts', 'admin'], 'string', 'max' => 20],
            [['mobile'], 'string', 'max' => 11],
            ['mobile', 'match', 'pattern' => '/^1[0-9]{10}$/', 'message' => '手机号码格式错误'],
            [['mobile','name'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'name' => Yii::t('zhtfrontend', '渠道名称'),
            'contacts' => Yii::t('zhtfrontend', '联系人'),
            'mobile' => Yii::t('zhtfrontend', '电话'),
            'admin' => Yii::t('zhtfrontend', '负责人'),
            'admin_id' => Yii::t('zhtfrontend', '负责人id'),
            'created_at' => Yii::t('zhtfrontend', '创建时间'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        return $fields;
    }

    public function extraFields()
    {
        return [
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
        ]);
    }
}
